package com.exeample.appium.utils;

import io.appium.java_client.android.AndroidDriver;
import org.eclipse.jetty.util.log.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

//import static com.exeample.appium.IphtsccftAppium.deviceName;
public class OperationUtils {

    /**
     * 通过ClassLoader类静态方法加载
     */
    public static AndroidDriver getPropertiesByInputStream_Four() throws IOException {
        AndroidDriver driver;
        //ClassLoader默认加载路径就是classpath，规定不需要用/开头文件路径
        String path = "appium.properties";
        Properties properties = new Properties();
        InputStream inputStream = ClassLoader.getSystemResourceAsStream(path);
        properties.load(inputStream);

        String deviceName = properties.getProperty("alvin.device.deviceName");
        String automationName = properties.getProperty("alvin.device.automationName");
        String platformName = properties.getProperty("alvin.device.platformName");
        String platformVersion = properties.getProperty("alvin.device.platformVersion");
        String appPackage = properties.getProperty("alvin.device.appPackage");
        String appActivity = properties.getProperty("alvin.device.appActivity");
        String protocol = properties.getProperty("alvin.appium.protocol");
        //创建连接
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", deviceName);
        capabilities.setCapability("automationName", automationName);
        capabilities.setCapability("platformName", platformName);
        capabilities.setCapability("platformVersion", platformVersion);
        capabilities.setCapability("appPackage", appPackage);
        capabilities.setCapability("appActivity", appActivity);
        //没有新命令，appium30秒退出
        capabilities.setCapability("newCommandTimeout", "40");
        // 每次启动时覆盖session，否则第二次后运行会报错不能新建session
        capabilities.setCapability("sessionOverride", true);
        driver = new AndroidDriver(new URL(protocol), capabilities);
        return driver;
    }

    /**
     * 找到控件并且点击
     * @param driver
     * @param idName
     */
    public static void findAndClick(AndroidDriver driver, String idName) {
        try {
            driver.findElement(By.id(idName)).click();
        }catch (Exception e){
            Log.getLog().debug("查找id失败"+ idName,1);
        }
    }

    /**
     * 延时等待 单位秒
     * @param driver
     * @param s
     */
    public static void delay(AndroidDriver driver, int s) {
        driver.manage().timeouts().implicitlyWait(s, TimeUnit.SECONDS);
    }

    /**
     * 根据时间等待控件出现
     * @param driver
     * @param id
     * @param waitTime
     */
    public static boolean waitForVisible(AndroidDriver driver, String id, int waitTime) {
        for (int attempt = 0; attempt < waitTime; attempt++) {
            try {
                driver.findElement(By.id(id));
                return true;
            } catch (Exception e) {
                driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            }
        }
        return false;
    }

    /**
     * 是否发现控件id
     * @param driver
     * @param by
     * @return
     */
    public static boolean isIdShow(AndroidDriver driver, final By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 快速发送 文本 [这里用到了测试线程中的设备号]
     * @param str
     */
    public static void sendKeys(String str) {
        try {
            //Runtime.getRuntime().exec("adb -s "+ deviceName +" shell input text " + str);
        }catch (Exception e){

        }
    }
}
